package model;

import java.io.Serializable;

import javax.persistence.*;

import dtos.ExcursionDTO;
import dtos.ExcursionPersTPDTO;
import dtos.InvitationDTO;
import dtos.PersonalizedTPDTO;
import authentication.ejb.usermanagement.User;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;


/**
 * The persistent class for the personalizedTP database table.
 * 
 */
@Entity
@NamedQuery(name="PersonalizedTP.findAll", query="SELECT p FROM PersonalizedTP p")
public class PersonalizedTP implements Serializable {
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private int travelID;

	@Temporal(TemporalType.TIMESTAMP)
	private Date dateOfDeparture;

	@Temporal(TemporalType.TIMESTAMP)
	private Date dateOfReturn;

	private int nomberOfPeople;
	private boolean confirmed;

	
	//bi-directional many-to-one association to Invitation
	@OneToMany(mappedBy="personalizedTp")
	private List<Invitation> invitations;

	//bi-directional many-to-one association to ExcursionPersTP
	@OneToMany(mappedBy="personalizedTp",cascade={CascadeType.PERSIST})
	private List<ExcursionPersTP> excursionPersTps;

	//bi-directional many-to-many association to Excursion
	@ManyToMany
	@JoinTable(
		name="excursionPersTP"
		, joinColumns={
			@JoinColumn(name="travelID")
			}
		, inverseJoinColumns={
			@JoinColumn(name="excursionID")
			}
		)
	private List<Excursion> excursions;

	//bi-directional many-to-one association to Hotel
	@ManyToOne
	@JoinColumn(name="hotel")
	private Hotel hotelBean;

	//bi-directional many-to-one association to MeanOfTransportation
	@ManyToOne
	@JoinColumn(name="departureTransportation")
	private MeanOfTransportation meanOfTransportation1;

	//bi-directional many-to-one association to MeanOfTransportation
	@ManyToOne
	@JoinColumn(name="returnTransportation")
	private MeanOfTransportation meanOfTransportation2;

	//bi-directional many-to-one association to User
	@ManyToOne
	@JoinColumn(name="customerID")
	private User user;

	public PersonalizedTP() {
		//this.excursionPersTps = new LinkedList<ExcursionPersTP>();
		//this.excursions = new LinkedList<Excursion>();
	}
	
	public PersonalizedTPDTO getDTO(){
		PersonalizedTPDTO p = new PersonalizedTPDTO();
		p.setDateOfDeparture(this.dateOfDeparture);
		p.setDateOfReturn(this.dateOfReturn);
		p.setExcursions(this.getExcursionsDTO());
		p.setHotelBean(this.getHotelBean().getDTO());		
		p.setMeanOfTransportation1(this.meanOfTransportation1.getDTO());
		p.setMeanOfTransportation2(this.meanOfTransportation2.getDTO());
		p.setNomberOfPeople(this.nomberOfPeople);
		p.setTravelID(this.travelID);
		p.setUser(this.user.getUserDTO());
		p.setConfirmed(this.confirmed);
		p.setInvitations(getInvitationsDTO());
		p.setExcursionPersTps(getExcursionPersTpsDTO());

		
		return p;
		
	}
	
	public PersonalizedTPDTO getDTOWithoutInvitation(){
		PersonalizedTPDTO p = new PersonalizedTPDTO();
		p.setDateOfDeparture(this.dateOfDeparture);
		p.setDateOfReturn(this.dateOfReturn);
		//p.setExcursions(this.getExcursionsDTO());
		p.setHotelBean(this.getHotelBean().getDTO());		
		p.setMeanOfTransportation1(this.meanOfTransportation1.getDTO());
		p.setMeanOfTransportation2(this.meanOfTransportation2.getDTO());
		p.setNomberOfPeople(this.nomberOfPeople);
		p.setTravelID(this.travelID);
		p.setUser(this.user.getUserDTO());
		p.setConfirmed(this.confirmed);
		p.setExcursionPersTps(this.getExcursionPersTpsDTO());

		
		return p;
		
	}
	
	public PersonalizedTPDTO getDTOWithoutExcursionPersTP() {
		PersonalizedTPDTO p = new PersonalizedTPDTO();
		p.setDateOfDeparture(this.dateOfDeparture);
		p.setDateOfReturn(this.dateOfReturn);
		p.setExcursions(this.getExcursionsDTO());
		p.setHotelBean(this.getHotelBean().getDTO());		
		p.setMeanOfTransportation1(this.meanOfTransportation1.getDTO());
		p.setMeanOfTransportation2(this.meanOfTransportation2.getDTO());
		p.setNomberOfPeople(this.nomberOfPeople);
		p.setTravelID(this.travelID);
		p.setUser(this.user.getUserDTO());
		p.setConfirmed(this.confirmed);

		return p;
	}

	public int getTravelID() {
		return this.travelID;
	}

	public void setTravelID(int travelID) {
		this.travelID = travelID;
	}

	public Date getDateOfDeparture() {
		return this.dateOfDeparture;
	}

	public void setDateOfDeparture(Date dateOfDeparture) {
		this.dateOfDeparture = dateOfDeparture;
	}

	public Date getDateOfReturn() {
		return this.dateOfReturn;
	}

	public void setDateOfReturn(Date dateOfReturn) {
		this.dateOfReturn = dateOfReturn;
	}

	public int getNomberOfPeople() {
		return this.nomberOfPeople;
	}

	public void setNomberOfPeople(int nomberOfPeople) {
		this.nomberOfPeople = nomberOfPeople;
	}

	public List<Invitation> getInvitations() {
		return this.invitations;
	}
	
	public List<InvitationDTO> getInvitationsDTO() {
		List<InvitationDTO> res = new LinkedList<InvitationDTO>();
		for (Invitation i : this.invitations){
			res.add(i.getDTO());
		}
		return res;
		
	}

	public void setInvitations(List<Invitation> invitations) {
		this.invitations = invitations;
	}

	public Invitation addInvitation(Invitation invitation) {
		getInvitations().add(invitation);
		invitation.setPersonalizedTp(this);

		return invitation;
	}

	public Invitation removeInvitation(Invitation invitation) {
		getInvitations().remove(invitation);
		invitation.setPersonalizedTp(null);

		return invitation;
	}

	public List<Excursion> getExcursions() {
		return this.excursions;
	}
	
	public List<ExcursionDTO> getExcursionsDTO() {
		List<ExcursionDTO> res = new LinkedList<ExcursionDTO>();
		for (Excursion e : this.excursions){
			res.add(e.getDTO());
		}
		return res;
		
	}

	
	public List<ExcursionPersTP> getExcursionPersTps() {
		return this.excursionPersTps;
	}

	public void setExcursionPersTps(List<ExcursionPersTP> excursionPersTps) {
		this.excursionPersTps = excursionPersTps;
	}
	
	public List<ExcursionPersTPDTO> getExcursionPersTpsDTO(){
		List<ExcursionPersTPDTO> res = new LinkedList<ExcursionPersTPDTO>();
		for (ExcursionPersTP e : this.getExcursionPersTps()){
			res.add(e.getDTO());
		}
		return res;
	}

	public void setExcursions(List<Excursion> excursions) {
		this.excursions = excursions;
	}
	
	public void addExcursion(Excursion e) {
		getExcursions().add(e);

	}
	
	public void removeExcursion(Excursion e) {
		getExcursions().remove(e);

	}



	public Hotel getHotelBean() {
		return this.hotelBean;
	}

	public void setHotelBean(Hotel hotelBean) {
		this.hotelBean = hotelBean;
	}

	public MeanOfTransportation getMeanOfTransportation1() {
		return this.meanOfTransportation1;
	}

	public void setMeanOfTransportation1(MeanOfTransportation meanOfTransportation1) {
		this.meanOfTransportation1 = meanOfTransportation1;
	}

	public MeanOfTransportation getMeanOfTransportation2() {
		return this.meanOfTransportation2;
	}

	public void setMeanOfTransportation2(MeanOfTransportation meanOfTransportation2) {
		this.meanOfTransportation2 = meanOfTransportation2;
	}

	public User getUser() {
		return this.user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public boolean belongsTo(String username) {
		
		return (this.user.getUsername().equals(username));
	}

	public boolean getConfirmed(){
		return confirmed;
	}
	
	public void setConfirmed(boolean confirmed){
		this.confirmed = confirmed;
	}

	



}